package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.Document;
import com.hengxinguotong.zhihuichengjian.ui.document.DocumentManagementActivity;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/3/27.
 */

public class DocumentAdapter extends BaseAdapter {
    private List<Document> documents;
    private Context context;

    public DocumentAdapter(Context context, List<Document> documents) {
        this.context = context;
        this.documents = documents;
    }


    @Override
    public int getCount() {
        return documents.size();
    }

    @Override
    public Document getItem(int position) {
        return (Document) documents.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null || convertView.getTag() == null) {
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.item_document, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Document document = documents.get(position);
        switch (document.getNameType()){
            case "docx":
            case "doc":
            case "DOCX":
            case "DOC":
                holder.documentIcon.setImageResource(R.mipmap.word_icon);
                break;
            case "xls":
            case "xlsx":
            case "XLS":
            case "XLSX":
                holder.documentIcon.setImageResource(R.mipmap.excel_icon);
                break;
            case "pdf":
            case "PDF":
                holder.documentIcon.setImageResource(R.mipmap.pdf_icon);
                break;
            case "jpg":
            case "jpeg":
            case "png":
            case "JPG":
            case "JPEG":
            case "PNG":
                holder.documentIcon.setImageResource(R.mipmap.pic_icon);
                break;
            case "ppt":
            case "pptx":
            case "PPT":
            case "PPTX":
                holder.documentIcon.setImageResource(R.mipmap.ppt_icon);
                break;
            default:
                holder.documentIcon.setImageResource(R.mipmap.other_icon);
                break;
        }
        if(document.getName() != null && document.getName().length() > 18){
            holder.documentName.setText(document.getName().substring(0, 17) + "..." + document.getNameType());
        }else{
            holder.documentName.setText(document.getName());
        }
        holder.dateTv.setText(document.getCreateTime());
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.document_icon)
        ImageView documentIcon;
        @BindView(R.id.document_name)
        HXTextView documentName;
        @BindView(R.id.date_tv)
        HXTextView dateTv;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
